package org.lmw.mm.interceptor;

import org.lmw.mm.app.AppConfig;
import org.lmw.mm.dao.User;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StringKit;

public class GlobalInterceptor implements Interceptor {

	//截取cookie
	public void intercept(ActionInvocation ai) {
        Controller controller = ai.getController();
        if(controller.getSessionAttr("sessionUser") == null){
            String APPID = controller.getCookie(AppConfig.APPID);
            if(StringKit.notBlank(APPID)){
                String[] nameAndPwd = APPID.split("_");
                User u = null;
                if(nameAndPwd != null && nameAndPwd.length == 3){
                    u = User.dao.findBynp(nameAndPwd[0], nameAndPwd[2]);
                }
                if(u != null){
                	//tomcat下编译出错
                	//controller.getSession().setMaxInactiveInterval(1800);
                    controller.setSessionAttr("sessionUser", u);
                    controller.setSessionAttr("sUname", u.getStr("name"));
                }else{
                    ai.getController().removeCookie(AppConfig.APPID);
                }
            }
        }
        ai.invoke();
	}

}
